package com.chinaisafe.user.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.chinaisafe.user.entity.SysDept;
import com.chinaisafe.user.entity.SysDeptDimension;
import com.chinaisafe.user.mapper.SysDeptDimensionMapper;
import com.chinaisafe.user.mapper.SysDeptMapper;
import com.chinaisafe.user.service.SysDeptDimensionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinaisafe.user.vo.request.SysDeptDimensionReqVO;
import com.chinaisafe.user.vo.response.SysDeptDimensionRespVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import com.chinaisafe.base.common.exception.BizException;
import com.chinaisafe.base.mybatisplus.constant.MybatisplusConstants;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinaisafe.base.mybatisplus.util.MpQueryWrapperPageUtil;

import java.util.Arrays;
import java.util.Optional;
/**
 * SysDeptDimensionServiceImpl
 * Description：部门维度服务实现类
 *
 * @author tby
 * @date 2024-07-16 14:28:47
 */
@Service
public class SysDeptDimensionServiceImpl extends ServiceImpl<SysDeptDimensionMapper, SysDeptDimension> implements SysDeptDimensionService {

    @Resource
    private SysDeptMapper sysDeptMapper;
    @Override
    public IPage<SysDeptDimensionRespVO> getPageList(SysDeptDimensionReqVO sysDeptDimensionReqVO) {
        // 分页设置
        Page<SysDeptDimension> page = MpQueryWrapperPageUtil.buildWrapperPage(new Page<>(), sysDeptDimensionReqVO);
        // 查询条件构造
        QueryWrapper<SysDeptDimension> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysDeptDimension::getDeleted, MybatisplusConstants.LOGIC_NOT_DELETE_VALUE);
        return baseMapper.getPageList(page, queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysDeptDimensionRespVO add(SysDeptDimensionReqVO sysDeptDimensionReqVO) {
        // 新增和编辑逻辑相同的情况下 直接调用新增或编辑方法
        SysDeptDimension one = lambdaQuery().eq(SysDeptDimension::getCode, sysDeptDimensionReqVO.getCode()).or().eq(SysDeptDimension::getName, sysDeptDimensionReqVO.getName()).one();
        Assert.isNull(one, "code或name已存在");
        return this.addOrUpdate(sysDeptDimensionReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysDeptDimensionRespVO update(SysDeptDimensionReqVO sysDeptDimensionReqVO) {
        // 新增和编辑逻辑相同的情况下 直接调用新增或编辑方法
        SysDeptDimension oldData = lambdaQuery().eq(StrUtil.isNotBlank(sysDeptDimensionReqVO.getCode()),SysDeptDimension::getCode, sysDeptDimensionReqVO.getCode())
                .or().eq(StrUtil.isNotBlank(sysDeptDimensionReqVO.getName()),SysDeptDimension::getName, sysDeptDimensionReqVO.getName()).one();
        Assert.isTrue(oldData != null && !oldData.getId().equals(sysDeptDimensionReqVO.getId()), "code或name已存在");
        return this.addOrUpdate(sysDeptDimensionReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysDeptDimensionRespVO addOrUpdate(SysDeptDimensionReqVO sysDeptDimensionReqVO) {
        Long sysDeptDimensionId = sysDeptDimensionReqVO.getId();
        if (sysDeptDimensionId != null){
            // 编辑
            SysDeptDimension sysDeptDimension = baseMapper.selectById(sysDeptDimensionId);
            Optional.ofNullable(sysDeptDimension).orElseThrow(() -> BizException.build("未找到要修改的部门维度信息"));
        }
        SysDeptDimension sysDeptDimension2Save = SysDeptDimension.convertFromReqVo(sysDeptDimensionReqVO);
        boolean success = this.saveOrUpdate(sysDeptDimension2Save);
        if (success) {
            return sysDeptDimension2Save.convertToRespVO();
        } else {
            BizException.throwException("操作失败，请刷新后重试");
        }
        return null;
    }

    @Override
    public SysDeptDimensionRespVO getDetailById(Long sysDeptDimensionId) {
        SysDeptDimension sysDeptDimension = baseMapper.selectById(sysDeptDimensionId);
        Optional.ofNullable(sysDeptDimension).orElseThrow(() -> BizException.build("未找到对应部门维度信息"));
        return sysDeptDimension.convertToRespVO();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByIds(Long[] sysDeptDimensionIds) {
        Long selectCount = sysDeptMapper.selectCount(Wrappers.<SysDept>lambdaQuery().in(SysDept::getSysDeptDimensionId, Arrays.asList(sysDeptDimensionIds)));
        Assert.isTrue(selectCount == 0, "所选部门维度下存在部门，请先删除部门");
        if (sysDeptDimensionIds.length > 0) {
            this.removeByIds(Arrays.asList(sysDeptDimensionIds));
        }
    }
}
